<?php

namespace app\index\controller;

use app\index\model\BidModel;
use app\index\model\DaysModel;
use app\index\model\EmailModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowSetModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\HuiModel;
use app\index\model\MeetModel;
use app\index\model\NewsModel;
use app\index\model\NoticeModel;
use app\index\model\TaskModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\model\WenFaModel;
use app\index\model\WenFenModel;
use app\index\service\FlowService;
use app\index\service\OptService;
use app\index\service\XingService;
use think\Db;
use think\Exception;
use think\Request;

class Welcome extends Common
{

    public function get_shen_topinfo(){
        $data['tijiao'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['created_user'=>$this->userinfo['id'],'isturn'=>0])->count();

        $data['shenpi'] = FlowTaskModel::alias('task')
            ->field('bill.*')->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.comid'=>$this->userinfo['comid'], 'bill.deleted_user'=>0, 'task.deleted_user'=>0])
            ->where(['bill.isturn'=>1,'bill.status'=>0,'task.uid'=>$this->userinfo['id'],'task.state'=>1])
            ->group('bill.id')->count();

        $data['renru'] = TaskModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where('status','in',[1,3])->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`zhier`)"))->count();

        $data['xiaoxi'] = TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->count();

        $data['youjian'] = EmailModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['recid'=>$this->userinfo['id'],'reciddel'=>0,'isread'=>0])->count();

        $data['gongwen'] = 0;
        $gongwen =  WenFenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where('zhongdt','>',date("Y-m-d"))
            ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`fenuser`)"))->column('reader');
        foreach ($gongwen as $reader){
            if(!in_array($this->userinfo['id'], explode(',', $reader))){
                $data['gongwen'] += 1;
            }
        }

        $data['huiyi'] = MeetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where('startdt','gt',date('Y-m-d H:i:s'))
            ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`caner`)"))->count();

        $data['huibao'] = 0;
        $huibao =  HuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`porter`)"))->column('reader');
        foreach ($huibao as $reader){
            if(!in_array($this->userinfo['id'], explode(',', $reader))){
                $data['huibao'] += 1;
            }
        }

        return outputJson(2000,lang('code_2000'),$data);
    }


    //我的申请 and 待我处理  and  我的投标/工程项目
    public function get_shen_dai(){


        $bidmy = [];
        $fisBid = "touer=".$this->userinfo['id'];
        $whereBid = Db::raw($fisBid);
        $bidmy['shen'] = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])->where($whereBid)->count();
        $bidmy['tou'] = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'state'=>1])->where($whereBid)->count();
        $bidmy['kai'] = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'state'=>3])->where($whereBid)->count();
        $data['bidmy'] = $bidmy;


        $enginmy = [];
        $fisEngin = "find_in_set(".$this->userinfo['id'].",man_zhuan)";
        $fisEngin .= " or find_in_set(".$this->userinfo['id'].",man_zi)";
        $fisEngin .= " or find_in_set(".$this->userinfo['id'].",man_cheng)";
        $fisEngin .= " or find_in_set(".$this->userinfo['id'].",man_zhian)";
        $fisEngin .= " or man_xiang=".$this->userinfo['id'];
        $whereEngin = Db::raw($fisEngin);
        $enginmy['no'] = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'state'=>0])->where($whereEngin)->count();
        $enginmy['zai'] = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'state'=>3])->where($whereEngin)->count();
        $enginmy['jun'] = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'state'=>6])->where($whereEngin)->count();
        $data['enginmy'] = $enginmy;


        $data['my'] = FlowBillModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['deleted_user'=>0])
            ->where(['created_user'=>$this->userinfo['id']])
            ->where(['isturn'=>1])
            ->order('id DESC')
            ->limit(5)
            ->select()->toArray();
        foreach ($data['my'] as &$item){
            $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
        }

        $data['dai'] = FlowTaskModel::alias('task')
            ->field('bill.*')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['bill.status'=>0])
            ->where(['task.uid'=>$this->userinfo['id']])
            ->where(['task.state'=>1])
            ->order('task.created_at DESC')
            ->limit(5)
            ->select()->toArray();
        foreach ($data['dai'] as &$item){
            $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
            $item['created_at'] = date("Y-m-d H:i", strtotime($item['created_at']));
        }

        return outputJson(2000,lang('code_2000'),$data);
    }

    //通知公告 会议 新闻资讯
    public function get_notice(){
        $data['notice'] = NoticeModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['deleted_user'=>0])
            ->where(['status'=>1])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",jieuser) or jieuser='' or jieuser is null"))
            ->order('ding DESC,id DESC')
            ->limit(7)
            ->select()->toArray();
        foreach ($data['notice'] as &$item){
            $item['tongtype'] = (new XingService())->get_notice_tongtype_show($item['tongtype']);
            $item['created_at'] = date("Y-m-d H:i", strtotime($item['created_at']));
            $item['created_ymd'] = date("Y-m-d", strtotime($item['created_at']));
        }


        $data['meet'] = MeetModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['deleted_user'=>0])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",caner) or created_user=".$this->userinfo['id']))
            ->order('startdt DESC')
            ->limit(7)
            ->select()->toArray();
        foreach ($data['meet'] as &$item){
            $item['room'] = (new OptService())->get_base_showname('qi_meet_room', $item['room']);
            $item['created_at'] = date("m-d H:i", strtotime($item['startdt']));
            $item['status_name'] = (new XingService())->get_meet_status($item['startdt'], $item['enddt']);
        }

        $data['news'] = NewsModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['deleted_user'=>0])
            ->order('id DESC')
            ->limit(3)
            ->select()->toArray();
        foreach ($data['news'] as &$item){
            $item['newstype'] = (new OptService())->get_base_showname('qi_news_newstype', $item['newstype']);
            $item['created_ymd'] = date("Y-m-d", strtotime($item['created_at']));
        }


        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_days_week(Request $request){
        $dt = $request->post('dt','','string');
        $week = $request->post('week',0,'int');
        $strtotime = strtotime($dt);
        $strtotime = !$strtotime ? time() : $strtotime;

        if($week == 1){
            $strtotime -= 7*24*60*60;
        }
        if($week == 2){
            $strtotime += 7*24*60*60;
        }

        $data['data'] = $this->get_week($strtotime);
        $data['today'] = date("Y-m-d");
        $data['tomon'] = date("Y年m月", strtotime($data['data'][0]['date']));
        return outputJson(2000,lang('code_2000'),$data);
    }
    function get_week($time=0, $format = "Y-m-d"){
        $week = date('w', $time);
        if (empty($week)) {
            $week = 7;
        }
        for ($i = 0; $i <= 6; $i++) {
            $currDay = $i+1-$week;
            $data[$i]['date'] = date($format, strtotime("+{$currDay} days", $time));
            $data[$i]['num'] = (int)date("d", strtotime("+{$currDay} days", $time));

            $shi = DaysModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id'],'dt'=>$data[$i]['date']])->find();
            $data[$i]['shi'] = $shi ? true : false;
        }
        return $data;
    }



    public function get_days_shi(Request $request){
        $dt = $request->post('dt','','string');

        $data = DaysModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['uid'=>$this->userinfo['id']])
            ->where(['dt'=>$dt])
            ->order('time ASC')
            ->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function del_days(Request $request){
        $id = $request->post('id',0,'int');
        $days = DaysModel::where(['id'=>$id])->find();
        DaysModel::where(['id'=>$days['id'],'uid'=>$this->userinfo['id']])->delete();

        $data = DaysModel::where(['comid'=>$this->userinfo['comid']])
            ->where(['uid'=>$this->userinfo['id']])
            ->where(['dt'=>$days['dt']])
            ->order('time ASC')
            ->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function guan_days(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                DaysModel::where(['uid'=>$this->userinfo['id'],'dt'=>$param['dt']])->delete();
                foreach($list as $vv){
                    $vv['comid'] = $this->userinfo['comid'];
                    $vv['uid'] = $this->userinfo['id'];
                    DaysModel::insert($vv);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $dt = $request->get('dt','','string');
            $list = DaysModel::where(['comid'=>$this->userinfo['comid']])
                ->where(['uid'=>$this->userinfo['id']])
                ->where(['dt'=>$dt])
                ->order('time ASC')
                ->select()->toArray();

            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }





    public function get_shen_mob(){
        $data['dainum'] = FlowTaskModel::alias('task')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['bill.status'=>0])
            ->where(['task.uid'=>$this->userinfo['id']])
            ->where(['task.state'=>1])
            ->count();


        $data['finishnum'] = FlowTaskShenModel::alias('shen')
            ->join('flow_bill bill','bill.id=shen.billid')
            ->join('user user','user.id=bill.created_user')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.isturn'=>1])
            ->where(['shen.uid'=>$this->userinfo['id']])
            ->group('bill.id')
            ->count();

        $data['chaonum'] = FlowPromerModel::alias('promer')
            ->join('flow_bill bill','promer.billid = bill.id')
            ->join('user user','user.id=bill.created_user')
            ->join('user fen','fen.id=promer.uid')
            ->where(['bill.comid'=>$this->userinfo['comid']])
            ->where(['bill.deleted_user'=>0])
            ->where(['bill.created_user'=>$this->userinfo['id']])
            ->where(['bill.isturn'=>1])
            ->where(Db::raw("find_in_set(".$this->userinfo['id'].",domer)"))
            ->count();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_jinflow_mob(){
        $data = [];
        $jin = FlowBillModel::field('modenum,modename')->where(['deleted_user'=>0,'created_user'=>$this->userinfo['id']])
            ->group('modeid')->order(Db::raw("MAX(created_at) DESC"))->limit(4)->select()->toArray();
        foreach ($jin as $v){
            $data[] = $v;
        }

        foreach ($data as $key=>&$item){
            $set = FlowSetModel::where(['modenum'=>$item['modenum']])->find();
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id']])->find();
            $item['status'] = $exist['status'];
            $item['ispublish'] = false;
            if(FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$exist['id'],'status'=>1,'deleted_user'=>0])->find()){
                $item['ispublish'] = true;
            }
        }



        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_jinflow(){
        $data = [];

        $jin = FlowBillModel::field('modenum,modename')->where(['deleted_user'=>0,'created_user'=>$this->userinfo['id']])
            ->group('modeid')->order(Db::raw("MAX(created_at) DESC"))->limit(6)->select()->toArray();
        foreach ($jin as $v){
            $data[] = $v;
        }


        foreach ($data as $key=>&$item){
            $set = FlowSetModel::where(['modenum'=>$item['modenum']])->find();
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id']])->find();
            $item['status'] = $exist['status'];
            $item['ispublish'] = false;
            if(FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$exist['id'],'status'=>1,'deleted_user'=>0])->find()){
                $item['ispublish'] = true;
            }
        }

        return outputJson(2000,lang('code_2000'),$data);
    }





}